﻿#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
//#include <string>
#include <cassert>
#include <list>
#include <stdbool.h>
#include <algorithm>
#include <utility>
using namespace std;
namespace lnb
{
	template<class Iterator, class Ref, class Ptr>
	struct my_reverse_iterator//反向迭代器
	{
		typedef my_reverse_iterator<Iterator, Ref, Ptr> self;
		my_reverse_iterator(const Iterator& lt)
			:_lt(lt)
		{}
		self& operator++()
		{
			_lt--;
			return *this;
		}

		self operator++(int)
		{
			self ret = *this;
			_lt--;
			return ret;
		}

		self& operator--()
		{
			_lt++;
			return *this;
		}

		self& operator--(int)
		{
			self ret = *this;
			_lt++;
			return *this;
		}


		Ref operator*()
		{
			Iterator tmp = this->_lt;
			tmp--;
			return *tmp;
		}

		bool operator!=(const self& s)
		{
			return _lt != s._lt;
		}


		bool operator==(const self& s)
		{
			return _lt == s._lt;
		}

		Ptr operator->()
		{
			return _lt.operator->;
		}


		Iterator _lt;


	};
}
namespace lnb
{
	template<class T>
	struct list_node//带头双向链表的结点
	{
		list_node(const T& x = T())
			:_prv(nullptr),
			_next(nullptr),
			_data(x)
		{}
		list_node( T&& x)
			:_prv(nullptr),
			_next(nullptr),
			_data(forward<T>(x))
		{
			
		}

		list_node<T>* _prv;
		list_node<T>* _next;
		T _data;
	};

	template<class T, class Ref, class Ptr>
	struct _list_iterator//封装迭代器
	{
		typedef list_node<T>* code;
		typedef _list_iterator<T, Ref, Ptr> self;
		_list_iterator(code cd)
			:_cd(cd)
		{}

		self operator++(int)
		{
			self ret = *this;
			_cd = _cd->_next;
			return ret;
		}

		self& operator++()
		{
			_cd = _cd->_next;
			return *this;
		}

		bool operator!=(const self& s)const
		{
			return _cd != s._cd;
		}

		bool operator==(const self& s)const
		{
			return _cd == s._cd;
		}

		self operator--(int)
		{
			self ret = *this;
			_cd = _cd->_prv;
			return ret;
		}

		self& operator--()
		{
			_cd = _cd->_prv;
			return *this;
		}


		Ref operator*()
		{
			return _cd->_data;
		}

		//针对于自定义类型
		//struct AA
		//{
		//	 int a;
		//	 int b;
		//};
		//按理来说应该是it->->a;为了可读性，变成了it->a
		Ptr operator->()//特例
		{
			return &(_cd->_data);
		}


		code _cd;
	};



	template<class T>
	class list
	{
	public:
		typedef _list_iterator<T, T&, T*> iterator;//迭代器
		typedef _list_iterator<T, const T&, const T*> const_iterator;
		typedef my_reverse_iterator<const_iterator, const T&, const T*> const_reverse_iterator;
		typedef my_reverse_iterator<iterator, T&, T*> reverse_iterator;
		reverse_iterator rbegin()
		{
			return reverse_iterator(end());
		}

		reverse_iterator rend()
		{
			return reverse_iterator(begin());
		}


		const_reverse_iterator rend()const
		{
			return const_reverse_iterator(begin());
		}


		const_reverse_iterator rbegin()const
		{
			return const_reverse_iterator(end());
		}


		iterator begin()
		{
			return _list_iterator<T, T&, T*>(_head->_next);
		}

		iterator end()
		{
			return _list_iterator<T, T&, T*>(_head);
		}





		const_iterator begin()const
		{
			return _list_iterator<T, const T&, const T*>(_head->_next);
		}


		const_iterator end()const
		{
			return _list_iterator<T, const T&, const T*>(_head);
		}


		void insert(const iterator& pos, const T& x)
		{
			node* cur = new node(x);
			node* prv = pos._cd->_prv;
			node* next = pos._cd;
			prv->_next = cur;
			cur->_prv = prv;
			next->_prv = cur;
			cur->_next = next;
		}


		void erase(const iterator& pos)
		{
			assert(pos != end());
			node* prv = pos._cd->_prv;
			node* next = pos._cd->_next;
			prv->_next = next;
			next->_prv = prv;
			delete pos._cd;
		}


		typedef list_node<T> node;
		list()
		{
			Init();
		}

		void Init()
		{
			_head = new node;
			_head->_prv = _head;
			_head->_next = _head;
		}

		void empty()
		{
			iterator it = begin();
			while (it != end())
			{
				erase(it++);
			}
		}

		~list()
		{
			empty();
			delete _head;
			_head = nullptr;
		}

		list(const list<T>& lt)
		{
			Init();
			for (auto e : lt)
			{
				push_back(e);

			}
		}


		void my_swap(list<T>& lt)
		{
			std::swap(_head, lt._head);

		}


		list<T>& operator=(list<T> lt)
		{
			my_swap(lt);
			return *this;
		}





		void push_back(const T& x)
		{
			node* cur = new node(x);
			node* prv = _head->_prv;
			cur->_next = _head;
			_head->_prv = cur;
			cur->_prv = prv;
			prv->_next = cur;
		}
		void push_back(T&& x)
		{
			cout << "push_back(T&& x)" << endl;
			node* cur = new node(forward<T>(x));
			node* prv = _head->_prv;
			cur->_next = _head;
			_head->_prv = cur;
			cur->_prv = prv;
			prv->_next = cur;
			
		}

	private:
		node* _head;
	};
}
namespace lnb
{
	class string
	{
	public:
		typedef char* iterator;
		iterator begin()
		{
			return _str;
		}
		iterator end()
		{
			return _str + _size;
		}
		string(const char* str = "")
			:_size(strlen(str))
			, _capacity(_size)
		{
			//cout << "string(char* str)" << endl;
			_str = new char[_capacity + 1];
			strcpy(_str, str);
		}
		// s1.swap(s2)
		void swap(string& s)
		{
			::swap(_str, s._str);
			::swap(_size, s._size);
			::swap(_capacity, s._capacity);
		}
		// 拷贝构造
		string(const string& s)
			:_str(nullptr)
		{
			cout << "string(const string& s) -- 深拷贝" << endl;
			string tmp(s._str);
			swap(tmp);
		}
		// 赋值重载
		string& operator=(const string& s)
		{
			cout << "string& operator=(string s) -- 深拷贝" << endl;
			string tmp(s);
			swap(tmp);
			return *this;
		}
		// 移动构造
		string(string&& s)
			:_str(nullptr)
			, _size(0)
			, _capacity(0)
		{
			cout << "string(string&& s) -- 移动语义" << endl;
			swap(s);
		}
		// 移动赋值
		string& operator=(string&& s)
		{
			cout << "string& operator=(string&& s) -- 移动语义" << endl;
			swap(s);
			return *this;
		}
		~string()
		{
			delete[] _str;
			_str = nullptr;
		}
		char& operator[](size_t pos)
		{
			assert(pos < _size);
			return _str[pos];
		}
		void reserve(size_t n)
		{
			if (n > _capacity)
			{
				char* tmp = new char[n + 1];
				strcpy(tmp, _str);
				delete[] _str;
				_str = tmp;
				_capacity = n;
			}
		}
		void push_back(char ch)
		{
			if (_size >= _capacity)
			{
				size_t newcapacity = _capacity == 0 ? 4 : _capacity * 2;
				reserve(newcapacity);
			}
				_str[_size] = ch;
			++_size;
			_str[_size] = '\0';
		}

		void push_back(string&& str)
		{
			cout << "push_back--string&&" << endl;
			swap(str);
		}
		string operator+(char ch)
		//string& operator+=(char ch)
		{
			string tmp(*this);
			tmp.push_back(ch);
			return tmp;
		}
		string operator+=(char ch)
		{
			return *this;
		}
		const char* c_str() const
		{
			return _str;
		}
	private:
		char* _str;
		size_t _size;
		size_t _capacity; // 不包含最后做标识的\0
	};

	//string& to_string(int value)
	//{
	//	bool flag = true;
	//	if (value < 0)
	//	{
	//		flag = false;
	//		value = 0 - value;
	//	}
	//	string str;
	//		std::reverse(str.begin(), str.end());
	//	return str;
	//}
}

//void Fun(int& x) { cout << "左值引用" << endl; }
//void Fun(const int& x) { cout << "const 左值引用" << endl; }
//void Fun(int&& x) { cout << "右值引用" << endl; }
//void Fun(const int&& x) { cout << "const 右值引用" << endl; }
//// 模板中的&&不代表右值引用，而是万能引用，其既能接收左值又能接收右值。
//// 模板的万能引用只是提供了能够接收同时接收左值引用和右值引用的能力，
//// 但是引用类型的唯一作用就是限制了接收的类型，后续使用中都退化成了左值，
//// 我们希望能够在传递过程中保持它的左值或者右值的属性, 就需要用我们下面学习的完美转发
////void Fun(int x) { cout << "int x" << endl; }
//template<typename T>
//void PerfectForward(T&& t)
//{
//	
//	Fun(forward<T>(t));
//
//}
//int main()
//{
//	PerfectForward(10); 
//	int a=1;
//	PerfectForward(a); 
//	PerfectForward(std::move(a)); // 右值
//	const int b = 8;
//	PerfectForward(b);// const 左值
//	PerfectForward(std::move(b)); // const 右值
//	return 0;
//}

//int main()
//{
//	const lnb::string& str=lnb::to_string(12);
//	int a = 10;
//	int b = 20;
//
//
//
//
//	return 0;
//}


//int main()
//{
//	//lnb::string str1("hello world");
//	//lnb::string str2 = str1 + 1;
//	//lnb::list<lnb::string> li;
//	//li.push_back("hello world");
//	//int y = 10;
//	//int x(move(y));
//	//cout << x<< endl;
//	//li.push_back(move(str1));
//	//cout <<str1.c_str() << endl;
//	const lnb::string s;
//	return 0;
//}
//int main()
//{
//	/* int&& a = 10;
//	a = 1;
//	cout << a << endl;
//	int x = 10;
//	int&& y = move(x);
//	cout << y << endl;*/
//	//lnb::string str1("hello world");
//	//lnb::string str2 = (str1 +'a');
//	lnb::string str =lnb::to_string(1111);
//	cout << endl;
//	//str = lnb::string("1111");
//	//str = lnb::to_string(111);
//	//cout << str.c_str() << endl;
//	lnb:: string str1 = str += 1;
//	cout << endl;
//	//lnb::string str1;
//	//str1 = str + 1;
//	str1 = str +=1;
//	return 0;
//}